package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.photomovie.PhotoMovieFactory;
import com.stark.photomovie.render.GLTextureView;
import com.stark.pmu.IPhotoMovieView;
import com.stark.pmu.PhotoMoviePresenter;
import com.stark.pmu.bean.FilterItem;
import com.stark.pmu.bean.FilterType;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.MusicFilterAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.databinding.ActivityMusicAlbumBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;

public class MusicAlbumActivity extends BaseAc<ActivityMusicAlbumBinding> implements IPhotoMovieView {

    public static List<String> listPath = new ArrayList<>();
    private MusicFilterAdapter filterAdapter;
    private PhotoMoviePresenter mMoviePresenter;
    private int filterPos = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_music_album;
    }

    @Override
    protected void initView() {
        mDataBinding.ivMusicAlbumBack.setOnClickListener(v -> onBackPressed());
        //设置音乐相册
        if (mMoviePresenter == null) {
            mMoviePresenter = new PhotoMoviePresenter(mContext, this);
            mMoviePresenter.setTransfer(PhotoMovieFactory.PhotoMovieType.SCALE_TRANS);
        }
        mMoviePresenter.attachView(this);
        mMoviePresenter.setPhotos(listPath);
        mDataBinding.ivMusicAlbumSave.setOnClickListener(this);
        mDataBinding.llFilter.setOnClickListener(this);
        mDataBinding.llMusic.setOnClickListener(this);
        mDataBinding.ivFilterCancel.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llFilter://滤镜
                mDataBinding.llMusicAlbumFunc.setVisibility(View.VISIBLE);
                mDataBinding.llMusicAlbumBottom.setVisibility(View.GONE);
                break;
            case R.id.ivFilterCancel://滤镜返回
                mDataBinding.llMusicAlbumFunc.setVisibility(View.GONE);
                mDataBinding.llMusicAlbumBottom.setVisibility(View.VISIBLE);
                break;
            case R.id.llMusic://音乐
                Intent intent = new Intent(this, SelMusicActivity.class);
                startActivityForResult(intent, Content.REQUEST_MUSIC);
                break;
            case R.id.ivMusicAlbumSave://保存
                save();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        filterAdapter.getItem(filterPos).setSelected(false);
        filterPos = position;
        filterAdapter.getItem(position).setSelected(true);
        filterAdapter.notifyDataSetChanged();
        //设置滤镜
        mMoviePresenter.setFilter(filterAdapter.getItem(position));
    }

    @Override
    protected void initData() {
        getFilterData();
    }

    private void getFilterData() {
        List<FilterItem> listFilter = new ArrayList<>();
        listFilter.add(new FilterItem(R.drawable.mfilter_1, getString(R.string.music_filter1), FilterType.NONE));
        listFilter.add(new FilterItem(R.drawable.mfilter_2, getString(R.string.music_filter2), FilterType.GRAY));
        listFilter.add(new FilterItem(R.drawable.mfilter_3, getString(R.string.music_filter3), FilterType.SNOW));
        listFilter.add(new FilterItem(R.drawable.mfilter_4, getString(R.string.music_filter4), FilterType.KUWAHARA));
        listFilter.add(new FilterItem(R.drawable.mfilter_5, getString(R.string.music_filter5), FilterType.LUT1));
        listFilter.add(new FilterItem(R.drawable.mfilter_6, getString(R.string.music_filter6), FilterType.LUT2));
        listFilter.add(new FilterItem(R.drawable.mfilter_7, getString(R.string.music_filter7), FilterType.CAMEO));
        listFilter.add(new FilterItem(R.drawable.mfilter_8, getString(R.string.music_filter8), FilterType.LUT3));
        listFilter.add(new FilterItem(R.drawable.mfilter_9, getString(R.string.music_filter9), FilterType.LUT5));
        listFilter.add(new FilterItem(R.drawable.mfilter_10, getString(R.string.music_filter10), FilterType.LUT4));
        listFilter.get(0).setSelected(true);
        mDataBinding.rvFilter.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        filterAdapter = new MusicFilterAdapter();
        filterAdapter.setList(listFilter);
        mDataBinding.rvFilter.setAdapter(filterAdapter);
        filterAdapter.setOnItemClickListener(this);
    }

    private void save() {
        mMoviePresenter.saveVideo(new PhotoMoviePresenter.ISaveCallback() {
            @Override
            public void onStart() {

            }

            @Override
            public void onProgress(int percent) {
                showDialog(getString(R.string.saving) + percent + getString(R.string.unit_percent));
            }

            @Override
            public void onEnd(boolean success, String filePath) {
                dismissDialog();
                if (success) {
                    String path = FileUtil.generateFilePath(Content.myMusicAlbum, "." + FileUtils.getFileExtension(filePath));
                    FileUtils.copy(filePath, path);
                    FileP2pUtil.copyPrivateVideoToPublic(mContext, filePath);//保存到手机相册
                    ToastUtils.showShort(getString(R.string.save_suc));
                    startActivity(HomeActivity.class);
                } else {
                    ToastUtils.showShort(getString(R.string.save_failure));
                }
            }
        });
    }

    /**
     * 设置音乐
     *
     * @param musicUri
     */
    protected void setMusic(Uri musicUri) {
        if (musicUri == null) return;
        showDialog(getString(com.stark.common.res.R.string.loading));
        RxUtil.create(new RxUtil.Callback<Boolean>() {
            @Override
            public void doBackground(ObservableEmitter<Boolean> emitter) {
                mMoviePresenter.setMusic(musicUri);
                emitter.onNext(true);
            }

            @Override
            public void accept(Boolean obj) {
                mMoviePresenter.setTransfer(mMoviePresenter.getMovieType());
                dismissDialog();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mMoviePresenter != null) {
            mMoviePresenter.destroy();
        }
    }

    @NonNull
    @Override
    public GLTextureView getGLTextureView() {
        return mDataBinding.glTextureView;
    }

    @Override
    public void onPreparingPhoto(float progress) {

    }

    @Override
    public void onPreparedPhoto(int prepared, int total) {

    }

    @Override
    public void onPreparedError() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == Content.REQUEST_MUSIC) {
            String path = data.getStringExtra(Content.musicPath);
            Uri uri = Uri.parse(path);
            setMusic(uri);
        }
    }

}